//
//  RxLoingViewController.swift
//  AKTest
//
//  Created by ZDST2018 on 2021/8/24.
//

import UIKit

class RxLoingViewController: UIViewController {
    
    @IBOutlet weak var accountTF: UITextField!
    @IBOutlet weak var passwordTF: UITextField!
    @IBOutlet weak var loginBtn: UIButton!
    lazy var bag = DisposeBag.init()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        accountTF.layer.borderWidth = 1
        let accountObs: Observable<Bool> = accountTF.rx.text.map({ (text :String?) -> Bool in
            //映射成bool
            if let str = text{
                return str.count >= 6
            }
            return false
        })
        accountObs.map({ (b: Bool) -> CGColor in
            //映射成颜色
            if b {
                return UIColor.green.cgColor
            }
            return UIColor.gray.cgColor
        }).subscribe { [weak self] eve in
            switch eve{
            case .next:
                self?.accountTF.layer.borderColor = eve.element!
            default:
                break
            }
        }.disposed(by: bag)
        
        passwordTF.layer.borderWidth = 1
        let passwordObs: Observable<Bool> =  passwordTF.rx.text.map({ (text :String?) -> Bool in
            //映射成bool
            if let str = text{
                return str.count >= 6
            }
            return false
        })
        passwordObs.map({ (b: Bool) -> CGColor in
            //映射成颜色
            if b {
                return UIColor.green.cgColor
            }
            return UIColor.gray.cgColor
        }).subscribe { [weak self] eve in
            switch eve{
            case .next:
                self?.passwordTF.layer.borderColor = eve.element!
            default:
                break
            }
        }.disposed(by: bag)
        
        
        let combineObs = Observable.combineLatest(accountObs, passwordObs)
        combineObs.map({ (b1: Bool, b2: Bool) -> Bool in
            return b1 && b2
        }).subscribe {[weak self] eve in
            switch eve{
            case .next:
                print(eve.element)
            self?.loginBtn.isEnabled = eve.element!
            default :
                break
            }
        }.disposed(by: bag)
        
        
        //       let combineObs = Observable.combineLatest(accountObs, passwordObs)
        //        combineObs.asObservable().map({ (b:Bool, c:Bool) -> [Bool] in
        //            return [b,c]
        //        }).map { (barr: Array<Bool>) -> Bool in
        //            return barr.reduce(true) { a, b in
        //                return a && b
        //            }
        //        }.subscribe {[weak self] eve in
        //            self?.loginBtn.rx.isEnabled = eve
        //        }.disposed(by: bag)
        
        
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destination.
     // Pass the selected object to the new view controller.
     }
     */
    
}
